<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>授权</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
</head>
<body>
<form class="layui-form">
    <div class="layui-form-item">
        <div class="layui-input-block">
            <div id="authTree"></div>
        </div>
    </div>
</form>
<script th:src="@{/layuiadmin/layui/layui.js}" charset="utf-8"></script>
<script>
//初始化
function initData(result) {
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        authtree: 'res/js/component/authtree',
    }).use(['authtree'],
    function() {
        var $ = layui.$,
        authtree = layui.authtree,
        layer = layui.layer;
        authtree.render('#authTree', result.dataList, {
            inputname: 'ids[]',
            layfilter: 'lay-check-auth',
            'theme': 'auth-skin-default',
            openall: true,
            autowidth: true
        });
    });
}
//确定提交
function submitData(roleId) {
    var $ = layui.$,
    authtree = layui.authtree,
    layer = layui.layer;
    var resourceIds = authtree.getChecked('#authTree');
    var userInfo = layui.data('admin').userInfo;
    var jsonData = JSON.stringify({"roleId": roleId, "resourceIds": resourceIds.join(","), 'operator': userInfo.username});
    $.ajax({
        url: '/admin/role/auth',
        type: 'post',
        contentType: "application/json",
        dataType: 'json',
        data: jsonData,
        success: function(data) {
            if (data.transactionStatus.success) {
                parent.layer.msg("授权成功");
            } else {
                parent.layer.msg(data.transactionStatus.replyText);
            }
        }
    });
}
//递归遍历：目前没用到
function loopTree(checkData, resourceIds) {
    for (var i = 0; i < checkData.length; i++) {
        var children = checkData[i].children;
        if (children.length == 0) {
            resourceIds.push(checkData[i].id);
        } else {
            resourceIds.push(checkData[i].id);
            loopTree(children, resourceIds);
        }
    }
    return resourceIds;
}
</script>
</body>
</html>